<?php
/**
 * Created by PhpStorm.
 * User: YongXuan.Huang
 * Date: 2017/10/3
 * Time: 19:59
 */

namespace Common\Common;


use Think\Model;

class Pagination
{
    /**
     * 数据
     *
     * @var array
     */
    public $data;

    /**
     * 一共多少条数据
     *
     * @var int
     */
    public $total;

    /**
     * 每页显示多少个
     *
     * @var int
     */
    public $per_page;

    /**
     * 当前页
     *
     * @var int
     */
    public $current_page;

    /**
     * 最后一页
     *
     * @var int
     */
    public $last_page;

    /**
     * @param $page
     * @param $limit
     * @param Model $model
     * @param null $where
     * @return Pagination
     */
    public static function make($page, $limit, $model, $where = null)
    {
        $pagination = new Pagination();
        $pagination->total = $model->where($where)->count();
        $pagination->current_page = $page;
        $pagination->per_page = $limit;
        $pagination->last_page = ceil($pagination->total / $pagination->per_page);
        if ($pagination->last_page == 1) {
            $start = 0;
        } else {
            $start = $pagination->current_page * $pagination->per_page - $pagination->per_page;
        }
        $pagination->data = $model->where($where) ->limit($start, $pagination->per_page)->select();
        return $pagination;
    }

    public function link(array $parameter = [], $class = 'pagination')
    {
        if ($this->last_page <= 1) {
            return;
        }
        $parameter = $this->parameter($parameter);
        echo '<ul class="' . $class . '">';
        $f = $this->current_page == 1 ? 'disabled' : '';
        $prev = $this->current_page - 1 > 1 ? $this->current_page - 1 : 1;
        echo "<li class='{$f}'><a href='{$parameter}page=1' {$f}>&laquo;</a></li>";
        echo "<li class='{$f}'><a href='{$parameter}page={$prev}' {$f}>&larr;</a></li>";
        $show = 8;
        $z = $show / 2;
        $start = $this->current_page <= $z ? 1 : $this->current_page - $z;
        $end = $start + $show;
        if ($end > $this->last_page) {
            $end = $this->last_page;
        }

        if ($show + $start > $this->last_page) {
            $start = $this->last_page - $show;
        }
        if ($start > 1) {
            echo '<li class="disabled"><a>...</a></li>';
        }
        if ($start < 1) {
            $start = 1;
        }
        for ($i = $start; $i <= $end; $i++) {
            $in = $i == $this->current_page ? 'active' : '';
            echo " <li class='{$in}'><a href='{$parameter}page={$i}'>$i</a></li>";
        }
        $d = $this->current_page == $this->last_page ? 'disabled' : '';
        $next = $this->current_page + 1 > $this->last_page ? $this->last_page : $this->current_page + 1;
        if ($end < $this->last_page) {
            echo '<li class="disabled"><a>...</a></li>';
        }
        echo "<li class='{$d}'><a href='{$parameter}page={$next}' {$d}>&rarr;</a></li>";
        echo "<li class='{$d}'><a href='{$parameter}page={$this->last_page}' {$d}>&raquo;</a></li>";
        echo "</ul>";
    }

    private function parameter(array $data)
    {
        $parameter = '?';
        foreach ($data as $key => $value) {
            $parameter .= "$key=$value&";
        }
        return $parameter;
    }
}